
package CSS::Preprocessor::ParseError;

use strict;
use overload '""' => sub{$_[0]->as_string};
use fields qw(
    message
    position
);

sub new {
    my ($class, $message, $position) = @_;

    my $self = fields::new($class);
    $self->{message} = $message;
    $self->{position} = $position;

    return $self;
}

sub new_expecting_but_got {
    my ($class, $expecting, $token_got) = @_;

    my $message = "Expecting $expecting but got ".($token_got ? $token_got : "EOF");
    my $position = $token_got ? $token_got->position : undef;

    return $class->new($message, $position);
}

sub message {
    return $_[0]->{message};
}

sub position {
    return $_[0]->{position};
}

sub as_string {
    my $self = shift;
    if ($self->position) {
        return $self->message." at ".$self->position;
    }
    else {
        return $self->message;
    }
}


1;
